package cn.net.isir.controller.manager;

import cn.net.isir.common.ResultObject;
import cn.net.isir.entity.Pics;
import cn.net.isir.service2.PicsService;
import cn.net.isir.utils.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
 * @author liuxubo
 * @filename GalleryController.java
 * @package cn.net.isir.controller.manager
 * @email lxb@isir.net.cn
 * @date 2018/12/28
 * @comment
 * @since 1.8
 */
@RestController
@RequestMapping("/gallery")
public class GalleryController {
    //声明日志对象
    private static Logger logger = Logger.getLogger(GalleryController.class);
    //    @Resource
//    private GalleryService galleryService;
    @Resource
    private PicsService picsService;

    /**
     * 图库
     *
     * @param request 请求
     * @return 结果
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResultObject list(HttpServletRequest request) {
        String urlParam = request.getParameter("name");
        String pageNumStr = request.getParameter("pageNum");
        String pageSizeStr = request.getParameter("pageSize");
        int pageNum = 1;
        int pageSize = 30;
        //转换默认参数
        try {
            pageNum = Integer.parseInt(pageNumStr);
            pageSize = Integer.parseInt(pageSizeStr);
        } catch (Exception ex) {
            logger.error("page info convert error", ex);
        }
        //URLDecoder解码，防止中文乱码
        String name = null;
        if (StringUtils.isNotBlank(urlParam)) {
            try {
                name = java.net.URLDecoder.decode(urlParam, StandardCharsets.UTF_8.name());
                logger.info("图库搜索关键字:" + name);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        // 加入搜索条件
        Pics pics = new Pics();
        if (name != null) {
            pics.setGoodsName(name);
        }
        return new ResultObject(200, "success", picsService.selectPageList(pics, pageNum, pageSize));
    }

    /*
     * 图库
     *
     * @param request 请求
     * @return 结果
     */
//    @RequestMapping(value = "/list2", method = RequestMethod.GET)
//    public ResultObject list2(HttpServletRequest request) {
//        String urlParam = request.getParameter("name");
//        //URLDecoder解码，防止中文乱码
//        String name = null;
//        if (StringUtils.isNotBlank(urlParam)) {
//            try {
//                name = java.net.URLDecoder.decode(urlParam, StandardCharsets.UTF_8.name());
//                logger.info("图库搜索关键字:" + name);
//            } catch (UnsupportedEncodingException e) {
//                e.printStackTrace();
//            }
//        }
//        List<Goods> goodsList = galleryService.getAllShopGoods(TokenManager.getTokenId(), name);
//        // 去掉重复的
//        Map<String, Integer> idCount = new HashMap<>();
//        goodsList.forEach(g -> idCount.put(g.getPic(), idCount.get(g.getPic()) == null ? 1 : idCount.get(g.getPic()) + 1));
//        List<Goods> distinctGoodsList = goodsList.stream()
//                .filter(g -> {
//                    if (idCount.get(g.getPic()) != null) {
//                        if (idCount.get(g.getPic()) > 0) {
//                            idCount.put(g.getPic(), 0);
//                            return true;
//                        }
//                    }
//                    return false;
//                })
//                .collect(Collectors.toList());
//        return new ResultObject(200, "success", distinctGoodsList);
//    }
}
